<template>
    <div class="wrapper">
        <upload type="img" upload=false dataType="1" />
        <div class="default-title">反馈<span> (感谢您对ADS平台的支持! 我们很期待您的反馈意见为您提供更好的服务~ )</span></div>
        <div class="setting" v-model="form">
            <el-input
                    :maxlength="maxLength"
                    type="textarea"
                    :autosize="{ minRows: 10}"
                    placeholder="请输入反馈内容..."
                    v-model="form.content"
                    style="margin-top: 20px">
            </el-input>
            <div class="tip">剩余可添加<span>{{count}}</span>字</div>

            <div class="upload" v-if="fileList.length >= 5 ? false : true">
                <span @click="showUploadImg" style="cursor: pointer"><i class="iconfont icon-files"></i> 上传图片</span>
                <div class="el-upload__tip">支持bmp\jpg\png\jpeg格式的图片，最多可上传5个附件</div>
            </div>

            <ul id="photo-files">
                <li v-for="item in fileList" class="item">
                    <div @click="preview(item.url)" style="cursor: pointer;display: inline-block">
                        <i class="iconfont icon-files icon-files-extra"></i>
                        {{ item.name }}
                    </div>
                    <i class="iconfont icon-close" @click="deleteFile(item)"></i>
                </li>
            </ul>

            <div class="code">
                <el-input v-model="form.code" placeholder="输入验证码" class="code-input"></el-input>
                <img :src="imgUrl" class="code-img" @click="getCode">
            </div>
        </div>
        <div class="btn-commit">
            <el-button v-show="!feedbackStatus" class="yellow-button" @click="submitUpload">提交反馈</el-button>
            <el-button v-show="feedbackStatus" class="yellow-button" :loading="true">提交中</el-button>
        </div>

        <div v-if='maskShow' class="mask-head" @click='maskShowChange'>
            <div>
                <img :src="dialogImageUrl" alt="" @click.stop>
            </div>
        </div>
    </div>
</template>
<script>
    import Vue from 'vue'
    import upload from '~components/upload/upload.vue';
    import { mapGetters} from 'vuex';
    import {FEEDBACK_STATUS} from '../store/mutation-types/feedback/index'
    import { Input ,Button,Message} from 'element-ui'
    Vue.use(Input);
    Vue.use(Button);

    export default{
        components: {
            upload
        },
        head(){
            return{
                title: 'ADS-反馈'
            }
        },
        data(){
            return{
                maxLength: 3000,
                form:{
                    code: '',
                    content: ''
                },
                imgUrl: '',
                fileList:[],
                fileListCopy: [],
                maskShow: false,
                dialogImageUrl: ''
            }
        },
        methods:{
            getCode() {
                this.$store.dispatch('createCode')
                    .then((response)=>{
                        if(response && response.data){
                            this.imgUrl = response.config.url + '?' + response.config.data+ '&timestamp=' + new Date().getTime();
                        }
                    })
            },
            showUploadImg() {
                this.$store.commit('SHOW_POPOVER_UPLOAD_IMG', {
                    showPopoverUploadImg: true
                })
            },
            uploadImgSuccess(list){
                for(var i = 0;i<list.length;i++){
                    var obj = {};
                    obj['key'] = list[i].key;
                    obj['url'] = list[i].url;
                    obj['name'] = list[i].name;
                    obj['id'] = list[i].id;
                    this.fileListCopy.push(obj);
                    if(this.fileListCopy.length > 5){
                        Message.warning({
                            message: '最多可上传5个附件'
                        });
                        this.fileList = this.fileListCopy.slice(0,5);
                    }else {
                        this.fileList = this.fileListCopy;
                    }
                }
            },
            preview(url){
                this.dialogImageUrl = url;
                this.maskShow = true;
            },
            maskShowChange() {
                this.maskShow = false
            },
            deleteFile(item) {
                for(var i=0;i<this.fileList.length;i++){
                    if(this.fileList[i].id == item.id){
                        this.fileList.splice(i,1);
                        this.fileListCopy = this.fileList;
                        return;
                    }
                }
            },
            submitUpload() {
                if(!this.form.content) {
                    Message.warning({
                        message: '请输入反馈内容'
                    });
                    return;
                }
                if(!this.form.code) {
                    Message.warning({
                        message: '请输入验证码'
                    });
                    return;
                }
                this.$store.commit('FEEDBACK_STATUS',{feedbackStatus: true});
                var data = this.form;
                for(var i=0;i<this.fileList.length;i++){
                    data['option'+ (i+1)] = JSON.stringify(this.fileList[i]);
                }
                this.$store.dispatch('feedback' , data)
                    .then((response)=>{
                        var result = response.data;
                        if(result && result.code == 1){
                            Message.success({
                                message: '反馈成功'
                            });
                            this.$store.commit('FEEDBACK_STATUS',{feedbackStatus: false});
                            this.form={
                                code: '',
                                content: ''
                            };
                            this.fileList = [];
                            this.getCode();
                        }else {
                            this.$store.commit('FEEDBACK_STATUS',{feedbackStatus: false});
                            this.getCode();
                        }
                    });
            }
        },
        computed: {
            count: function () {
                let countNum = 3000 - this.form.content.length;
                return countNum;
            },
            ...mapGetters({
                'feedbackStatus': 'getFeedbackStatus'
            })
        },
        created() {
            this.getCode();
        }
    }
</script>

<style scoped lang="scss">
    .wrapper{
        padding: 0 58px;
        background-color: #fff;
        min-height: 594px;
        .default-title {
            font-size: 24px;
            height: 68px;
            line-height: 68px;
            border-bottom: 1px solid rgba(0,0,0,0.14);
            span {
                font-size: 17px;
                color: #666;
                margin-left: 10px;
            }
        }
        .setting {
            width: 952px;
            margin: 0 auto;
            .upload {
                font-size: 13px;
                color: #0686F9;
                margin-left: 20px;
            }
            .code {
                margin-top: 20px;
                height: 80px;
                width: 100%;
                .code-input {
                    width: 166px;
                    float: left;
                }
                .code-img {
                    width: 70px;
                    height: 32px;
                    margin-left: 20px;
                    float: left;
                }
            }
        }
        .btn-commit{
            text-align: center;
            padding-bottom: 80px;
        }
        .el-upload__tip{
            display: inline-block;
            margin-left: 20px;
        }
        .icon-files {
            font-size: 18px;
        }
        #photo-files {
            .icon-files-extra{
                color: #FFB100;
                margin-left: 20px;
            }
            .icon-close{
                float: right;
                margin-right: 20px;
                line-height: 40px;
                cursor: pointer;
            }
            .item {
                width: 100%;
                height: 40px;
                line-height: 40px;
                font-size: 13px;
                color: #666666;
                background: #F4F7F9;
                margin-top: 10px;
            }
        }
        .tip {
            font-size: 12px;
            color: #8391a5;
            width: 100%;
            text-align: right;
            height: 30px;
            line-height: 30px;
            margin-bottom: 20px;
            span {
                color: #0686F9;
            }
        }
        .mask-head {
            position: fixed;
            background-color: rgba(0, 0, 0, 0.84);
            width: 100%;
            height: 100%;
            right: 0;
            text-align: center;
            top: 0;
            z-index: 2020;
            div {
                width: 50%;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%,-50%);
                i {
                    position: absolute;
                    top: 0;
                    right: 0;
                    color: #fff;
                    transform: translate(-20%,-150%);
                }
                img {
                    max-width: 70%;
                }
            }
        }
    }
</style>